﻿var ScriptViewer = {};

ScriptViewer.CurrentPage = "#scriptPanel-TOC";

ScriptViewer.Init = function () {
	$("#scriptDialogChrome").draggable({
		handle: ".header"
	});

	$("#scriptDialogViewPort").resizable({
		resize: function (e, ui) {
			var scrollChrome = $("#scriptDialogChrome");

			var scrollContainer = $("#scriptDialogScrollContainer");
			var panels = scrollContainer.find(".panel");

			scrollChrome.width(ui.element.width() + 25);
			scrollChrome.height(ui.element.height() + 25);

			scrollContainer.width(panels.length * (ui.element.width() + 2));

			panels.width(ui.element.width())
				.find(".content").height(ui.element.height() - 54);
		}
	});

}


/*-------------------------------------------------------*/
ScriptViewer.InitHandlers = function () {

	$("#scriptDialogViewPort span.panelNavButton").click(function () {
		var $this = $(this);
		var scrollTo = $this.attr("scrollTo"); //the name of the panel that needs to be scrolled to.
		var loaderFunction = $(scrollTo).attr("loaderFunction"); //loaderFunction is a String. It is the name of the function in this ScriptViewer object to run if content has not yet been loaded into the panel defined in the scrollTo value.

		ScriptViewer.ScrollTo(scrollTo, loaderFunction)
	});

	/*-------------------------------------*/
	$("#scriptPanel-TOC").delegate("A", "click", function (e) {
		var scriptID = this.getAttribute("scriptid");

		ScriptViewer.LoadScript(scriptID);

		e.preventDefault();
	});

	/*-------------------------------------*/
	$("#scriptSearchBoxInput").keyup(function (e) {
		if (this.value.length > 0) {
			var criteria = this.value;
			ScriptViewer.SearchForScripts(criteria)
		}
		else {
			$("#scriptSearchBoxWait").hide();
		}
	});

	/*-------------------------------------*/
	$("#scriptPanel-Search").delegate("A", "click", function (e) {
		var scriptID = this.getAttribute("scriptid");

		ScriptViewer.LoadScript(scriptID);

		e.preventDefault();
	});

};


/*-------------------------------------------------------*/
ScriptViewer.LoadScript = function (scriptID) {
	var iframe = $("#scriptPanel-Script-ScriptContainer")[0];

	iframe.style.display = "none";
	$("#scriptPanel-Script-PleaseWait").show();

	ScriptViewer.ScrollTo("#scriptPanel-Script", "");

	setTimeout(function () {
		iframe.src = "ScriptReader.asp?ScriptID=" + scriptID;
	}, 0);


};

/*-------------------------------------------------------*/
ScriptViewer.CheckForContent = function (panel, loaderFunction) {
	var content = $(panel).find(".content").html();

	if (content == "" && loaderFunction) {
		ScriptViewer[loaderFunction]();
	}
};

/*-------------------------------------------------------*/
ScriptViewer.ScrollTo = function (panel, loaderFunction) {
	
	ScriptViewer.CheckForContent(panel, loaderFunction);

	$("#scriptDialogViewPort").scrollTo(panel, {
		duration: 300,
		axis: "x",
		offset: { left: -2 },
		easing: "swing"
	});

	ScriptViewer.CurrentPage = panel;
};

/*-------------------------------------------------------*/
ScriptViewer.LoadIndex = function () {
	var content = $("#scriptPanel-TOC").find(".content");

	$.ajax({
		url: "AJAX/ScriptReader.asp?method=GetScriptsGroupedByCategory",
		type: "POST"
	})
	.done(function (response) {
		if (response.Success == "true") {

			var html = [];

			for (var category in response.Data.Scripts) {
				var currentCategory = response.Data.Scripts[category];

				html.push(
					"<div class='categoryHeader'>"
						, category
					, "</div>"
					, "<ul category='" + category + "'>"
				);

				for (var script in currentCategory) {
					var currentScript = currentCategory[script];

					html.push(
						"<li scriptid='" + currentScript.ScriptID + "'>"
							, "<span class='ui-icon ui-icon-note'></span>"
							, "<a scriptid='" + currentScript.ScriptID + "'>"
								, currentScript.Title
							, "</a>"
						, "</li>"
					);
				}

				html.push("</ul>");
			}


			content.html(html.join(""));
		}
	})
    .fail(function () { })
    .always(function () { });

}

/*-------------------------------------------------------*/
ScriptViewer.SearchForScripts = function (criteria, categoryOnly, exactMatch, autoLoadOnOneResult) {

	var data = {
		CategoryOnly: categoryOnly || false
		, ExactMatch: exactMatch || false
		, Criteria: criteria || ""
		, AutoLoadOnOneResult: autoLoadOnOneResult || false
	}

	if (data.Criteria == "") return false;

	$("#scriptSearchBoxWait").show();

	$.ajax({
		url: "AJAX/ScriptReader.asp?method=SearchForScripts",
		type: "POST",
		data: data
	})
	.done(function (response) {
		if (response.Success !== "true") {
			var content = $("#scriptPanel-Search").find(".content")
				, html = ["<ul class='searchResults'>"]
				, scripts;
			//for (var item in response) { html.push(item + ": " + response[item] + "<br>"); }

			scripts = (response.Scripts || []);

			/* autoLoadOnOneResult */
			if (autoLoadOnOneResult && scripts.length == 1) {
				ScriptViewer.LoadScript(scripts[0].ScriptID);
				return false;
			}

			if (scripts.length == 0) {
				html.push("<li>No Scripts Found For Search Criteria '" + criteria + "'</li>");
				ScriptViewer.ScrollTo("#scriptPanel-Search", "");
			}

			for (var script in scripts) {
				var currentScript = scripts[script];

				html.push(
						"<li scriptid='" + currentScript.ScriptID + "'>"
							, "<span class='ui-icon ui-icon-note'></span>"
							, "<a scriptid='" + currentScript.ScriptID + "'>"
								, "<span class='scriptTitle'>" + currentScript.Title + "</span>"
								, "<br>"
								, "<span class='scriptType'>" + currentScript.MatchType + ": </span>"
								, "<span class='scriptMatchText'>" + currentScript.MatchText + "</span>"
							, "</a>"
						, "</li>"
					);
			}

			html.push("</ul>");
			content.find("UL").remove();  //if search has already been done then there bee the old UL so instead of fancy checks and conditions, we'll just remove it if its there.
			content.append(html.join(""));
		}
	})
    .fail(function () { })
    .always(function () {
    	$("#scriptSearchBoxWait").hide();
    });

	HomeSmartAfterHours.ShowScriptsWindow();

};



/*=======================================================*/
$(document).ready(function () {
	ScriptViewer.Init();
	ScriptViewer.InitHandlers();
});